package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.user.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService{

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 条件查询
     *
     * @param addressBook
     * @return
     */
    public List<AddressBook> list(AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId())
                .eq(null != addressBook.getPhone(), AddressBook::getPhone, addressBook.getPhone())
                .eq(null != addressBook.getIsDefault(), AddressBook::getIsDefault, addressBook.getIsDefault());
        List<AddressBook> addressBooks = addressBookMapper.selectList(wrapper);
        return addressBooks;
    }

    /**
     * 新增地址
     * @param addressBook
     */
    @Override // 重写 IService 中的 save方法
    public boolean save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        return super.save(addressBook);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
        public AddressBook getById(Long id) {
            return addressBookMapper.selectById(id);
        }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    public void update(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getId, addressBook.getId());
        addressBookMapper.update(addressBook, updateWrapper);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     */
    @Transactional
    public void setDefault(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        LambdaUpdateWrapper<AddressBook> updateWrapperOld = new LambdaUpdateWrapper<>();
        updateWrapperOld.eq(AddressBook::getUserId, BaseContext.getCurrentId())
                        .set(AddressBook::getIsDefault, 0);
        addressBookMapper.update(addressBook, updateWrapperOld);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        LambdaUpdateWrapper<AddressBook> updateWrapperNew = new LambdaUpdateWrapper<>();
        updateWrapperNew.eq(AddressBook::getId, addressBook.getId())
                .set(AddressBook::getIsDefault, 1);
        addressBookMapper.update(addressBook, updateWrapperNew);
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }


}